package com.only4play.web.resource;

import com.only4play.common.constants.ErrorCodeEnum;
import com.only4play.common.exception.ApplicationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liyuncong
 * @version 1.0
 * @file RedisTestResource
 * @brief RedisTestResource
 * @details RedisTestResource
 * @date 2024-01-18
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-01-18               liyuncong          Created
 */

@RestController
@RequestMapping("/redis")
public class RedisTestResource {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/set")
    public ResponseEntity<Boolean> set(
        @RequestParam("value") String value
    ) {
        try {
            redisTemplate.boundValueOps("hello").set(value);
            return ResponseEntity.ok(true);
        } catch (Exception e) {
            return ResponseEntity.ok(false);
        }
    }

    @PostMapping("/get")
    public ResponseEntity<Boolean> get(
        @RequestParam("value") String value
    ) {
        try {
            String object = (String) redisTemplate.boundValueOps("hello").get();
            return ResponseEntity.ok(object.equals(value));
        } catch (Exception e) {
            throw new ApplicationException(ErrorCodeEnum.NOT_FOUND_ERROR, e);
        }
    }
}
